package com.xckj.ibox.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.TypedValue;

import com.xckj.ibox.App;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.ui.activity.LoginActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ChenYao
 * @Describe: Android工具类
 * @Date: 2023/6/11 11:16
 */
public class AndroidUtil {
    private static long lastClickTime;
    private static final int CLICK_REPEAT_TIME = 400;

    /**
     * 判断用户连续点击按钮间隔
     *
     * @return 是否连续点击
     */
    public static boolean isFastDoubleClick() {
        long time = SystemClock.uptimeMillis();
        if (time - lastClickTime < CLICK_REPEAT_TIME) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    /**
     * dp转px
     */
    public static int dip2px(float dpValue) {
        Context context = App.getInstance();
        final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 校验手机号格式
     *
     * @param phoneNumber 手机号码
     * @return true 格式正确 false 格式错误
     */
    public static boolean isPhoneNumber(String phoneNumber) {
        String regExp = "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[0-9])|(147,145))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phoneNumber);
        return !m.matches();
    }

    /**
     * 是否登录
     *
     * @return true 登录 false 未登录
     */
    public static boolean isLogin() {
        boolean isLogin = TextUtils.isEmpty(SpUtil.getString(App.getInstance(), Constant.APP_TOKEN)) && (TextUtils.isEmpty(SpUtil.getString(App.getInstance(), Constant.IS_AUTO_LOGIN)) || "0".equals(SpUtil.getString(App.getInstance(), Constant.IS_AUTO_LOGIN)));
        if (isLogin) {
            App.getInstance().startActivity(new Intent(App.getInstance(), LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            return false;
        } else {
            return true;
        }
    }

    /**
     * 登录状态
     *
     * @return true 已登录 false 未登录
     */
    public static boolean loginState() {
        boolean isLogin = TextUtils.isEmpty(SpUtil.getString(App.getInstance(), Constant.APP_TOKEN)) && (TextUtils.isEmpty(SpUtil.getString(App.getInstance(), Constant.IS_AUTO_LOGIN)) || "0".equals(SpUtil.getString(App.getInstance(), Constant.IS_AUTO_LOGIN)));
        return !isLogin;
    }

    /**
     * 隐藏手机号中间四位
     *
     * @param mobile 手机号码
     * @return 隐藏后的手机号
     */
    public static String mobileEncrypt(String mobile) {
        final String phoneBlurRegex = "(\\d{3})\\d{4}(\\d{4})";
        /* 手机号脱敏替换正则 */
        final String phoneBlurReplaceRegex = "$1****$2";
        /* 手机号格式校验 * @param phone * @return */
        if (Constant.MOBILE_LENGTH != mobile.length()) {
            return mobile;
        }
        return mobile.replaceAll(phoneBlurRegex, phoneBlurReplaceRegex);
    }

    /**
     * 获取版本名字
     *
     * @return 版本名字
     */
    public static String getVersionName() {
        PackageManager manager = App.getInstance().getPackageManager();
        String versionName = null;
        try {
            PackageInfo info = manager.getPackageInfo(App.getInstance().getPackageName(), 0);
            versionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "V" + versionName;
    }
}
